package com.snapan.service.impl;

import com.snapan.dao.UserDao;
import com.snapan.entity.User;
import com.snapan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户表服务实现类
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User getById(Long id) {
        return userDao.selectById(id);
    }

    @Override
    public List<User> getAll() {
        return userDao.selectAll();
    }

    @Override
    public boolean add(User user) {
        return userDao.insert(user) > 0;
    }

    @Override
    public int update(String username, String avatar, Long id) {
        return userDao.update(username,avatar,id);
    }


    @Override
    public boolean delete(Long id) {
        return userDao.deleteById(id) > 0;
    }

    @Override
    public Long getUsedSpace(Long userId) {
        Long sum = userDao.sumUsedSpaceByUserId(userId);
        return sum == null ? 0L : sum;
    }

    @Override
    public Long getTotalSpace(Long userId) {
        User user = userDao.selectById(userId);
        return user == null || user.getTotalSpace() == null ? 0L : user.getTotalSpace();
    }

    @Override
    public boolean existsByEmail(String email) {
        if(userDao.existsByEmail(email)!=null){
            return true;
        }
        return false;
    }

    @Override
    public Integer register(User user) {
        return userDao.register(user);
    }

    @Override
    public User selectByEmailAndPassword(String email, String password) {
        return userDao.selectByEmailAndPassword(email,password);
    }

    @Override
    public int updatePasswordByEmail(String email, String newPassword) {

        return userDao.updatePasswordByEmail(email,newPassword);
    }



}




















